package com.hua.nailcampus.acl.controller;

import com.hua.nailcampus.acl.model.Friends;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.vo.UserLoginVo;
import com.hua.nailcampus.acl.model.vo.UserVo;
import com.hua.nailcampus.acl.service.FriendsService;
import com.hua.nailcampus.acl.service.UserService;
import com.hua.nailcampus.constant.RedisConst;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.MD5;
import com.hua.nailcampus.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author:chenjianhua
 * @Date:2023/11/17 16:51
 * @Version 1.8
 */
@CrossOrigin
@RestController
@RequestMapping("nailCampus/index")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private FriendsService friendsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/register")
    public Result registerUser(@RequestBody User user){
        UserVo userVo = userService.getUserByPhone(user.getPhone());
        if(userVo!= null){
            return Result.fail("该号码已被注册");
        }
//        密码 MD5加密再存入数据库
        String password = user.getPassword();
        String newPassword = MD5.encrypt(password);
        user.setPassword(newPassword);
        userService.save(user);
        userService.createRole(user);
//        只要用户注册成功，自己与自己肯定是好友关系，所以直接存入好友关系数据库
        Friends friend = new Friends();
        friend.setUId(user.getId());
        friend.setFId(user.getId());
        friend.setFName(user.getUserName());
        friendsService.addFriend(friend);
        return Result.ok(null);
    }

    @PostMapping("/login")
    public Result loginUserByPassword(@RequestBody UserLoginVo userLoginVo){
        Map<String,Object> map = new HashMap<>();
//        密码登录
        if("1".equals(userLoginVo.getLoginType())){
            UserVo userVo = userService.loginUserByPassword(userLoginVo);
            if (userVo == null){
                return Result.fail("用户名或者密码错误");
            }
            map.put("user", userVo);
            String token = JwtHelper.createToken(userVo.getId(),userVo.getUserName());
            map.put("token", token);
            redisTemplate.opsForValue().set(RedisConst.USER_TOKEN + userVo.getId(),token,RedisConst.TOKEN_TIMEOUT,TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(RedisConst.USER_KEY_LOGIN + userVo.getId(), userVo, RedisConst.USER_KEY_TIMEOUT, TimeUnit.MINUTES);
            return Result.ok(map);
        }
//        短信登录
        if("0".equals(userLoginVo.getLoginType())){

        }
        return Result.ok(map);
    }

    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        userService.logout(userId);
        return Result.ok(null);
    }
}
